import type Upload from './upload.vue'
import type { ExtractPropTypes } from 'vue'
import type { UploadUserFile as ElUploadUserFile } from 'element-plus'
import { isArray } from '@/utils'
import {
  exportExcelData,
  CommonUploadFile,
  keyWordExcelExportTemplate
} from '@/api/common'

export type UploadFile = ElUploadUserFile & {
  url: string
  response?: CommonUploadFile
}

export interface UploadRule {
  validator: (file: File) => boolean
  message: string
}

export const uploadExcelProps = {
  templateUrl: [String, Function],
  // 接收上传的文件类型
  accept: {
    type: String,
    default: 'all'
  },
  // 要用原始请求
  excelExportHttp: {
    type: Function,
    default: keyWordExcelExportTemplate
  },
  http: {
    type: Function,
    default: exportExcelData
  },
  httpFileKey: {
    type: String,
    default: 'file'
  },
  data: {
    type: Object,
    default: () => {}
  },
  exportData: {
    type: Object,
    default: () => {}
  },
  importText: {
    type: String,
    default: '批量导入'
  },
  tip: {
    type: String,
    default: '点击或将文件拖拽到这里上传'
  }
} as any

export const uploadEmits = {
  'update:modelValue': (fileList: UploadFile[]) => isArray(fileList),
  change: (fileList: UploadFile[]) => isArray(fileList),
  reload: () => true,
  cancel: () => true
}

export type UploadProps = ExtractPropTypes<typeof uploadExcelProps>
export type UploadEmits = typeof uploadEmits

export type UploadInstance = InstanceType<typeof Upload>
